package com.allianz.webapps.secondController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.ClaimService;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value = "myPolicy")
public class ClaimController {
	
	@Autowired
	private ClaimService claimService;
	
	@Autowired
	private ObjectMapper objectMapper;

	/**
	 * 我的保单,理赔按条件查询保单
	 * @param jsonParam
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/queryClaims.action
	 */
	@RequestMapping(value = "queryClaims")
	@ResponseBody
	public List<Map<String, Object>> queryClaims(@RequestParam(value = "jsonParam") String jsonParam, HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar gc = new GregorianCalendar();
		if(map.get("regDateFrom").toString() != null && !"".equals(map.get("regDateFrom").toString().trim())
		&& map.get("regDateTo").toString() != null && !"".equals(map.get("regDateTo").toString().trim())) {
			Date regDateFrom = sdf.parse(map.get("regDateFrom").toString());
			Date regDateTo = sdf.parse(map.get("regDateTo").toString());
			if(regDateFrom.getTime() > regDateTo.getTime()) {
				throw new Exception("受理日期的起始日期不能大于截止日期");
			} else {
				if("C".equals(map.get("claimStatus").toString()) || "F".equals(map.get("claimStatus").toString())) {
					regDateTo = new Date();
					gc.setTime(regDateTo);
					gc.add(5, -5);
					regDateFrom = gc.getTime();
					map.put("regDateFrom", sdf.format(regDateFrom));
					map.put("regDateTo", sdf.format(regDateTo));
				} else {
					gc.setTime(regDateFrom);
					gc.add(5, 90);
					regDateFrom = gc.getTime();
					if(regDateFrom.getTime() < regDateTo.getTime()) {
						throw new Exception("受理日期查询区间不能超过90天");
					}
				}
			}
		}
		User user = (User) session.getAttribute("username");
		map.put("agentId", Long.parseLong(user.getAgentId()));
//		map.put("agentId", 70912);
		try{
			return claimService.queryClaims(map);
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("获取理赔保单列表信息失败,请重试");
		}
	}
	
	/**
	 * 获取理赔状态图
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/myPolicy/getClaimChart.action
	 */
	@RequestMapping(value = "getClaimChart")
	@ResponseBody
	public List<Map<String, Object>> getClaimChart(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try {
			return claimService.getClaimChart(Long.parseLong(user.getAgentId()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取理赔状态图,请重试");
		}
	}
}
